package com.dwx.gmall.product.controller;

import com.dwx.gmall.common.result.Result;
import com.dwx.gmall.product.component.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
@RequestMapping("/admin/product")
public class FileUploadController {
    @Autowired
    MinioService minioService;

    @PostMapping("/fileUpload")
    public Result<String > uploadFile(@RequestPart("file") MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String upload = minioService.upload(inputStream, null, originalFilename);
            return Result.ok(upload);
        } catch (Exception e) {
            return Result.fail("网络异常");
        }
    }
}
